﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.Collections;
namespace XWord.WPF.Behaviors
{
    public class AttachPropertyBehavior : Behavior<FrameworkElement>
    {
        protected override void OnAttached()
        {

            base.OnAttached();

        }

        public double Productivity
        {
            get { return (double)GetValue(ProductivityProperty); }
            set { SetValue(ProductivityProperty, value); }
        }

        public static readonly DependencyProperty ProductivityProperty =
            DependencyProperty.Register("Productivity", typeof(double), typeof(AttachPropertyBehavior),
            new UIPropertyMetadata(new PropertyChangedCallback((sender,args)=>{

                var a = sender as AttachPropertyBehavior;
            })));



        public object Entity
        {
            get { return (object)GetValue(EntityProperty); }
            set { SetValue(EntityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Entity.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EntityProperty =
            DependencyProperty.Register("Entity", typeof(object), typeof(AttachPropertyBehavior),
            new UIPropertyMetadata(new PropertyChangedCallback((sender, args) =>
            {
                if (args.NewValue == null) return;
                var behavior = sender as AttachPropertyBehavior;
                behavior.AttachProperty();
            })));

        private void AttachProperty()
        {
            StackPanel panel = AssociatedObject as StackPanel;
            var dictionary = Entity as IDictionary;

            if (dictionary.Contains(DataContext))
            {
                panel.Children[1].SetValue(TextBox.TextProperty, dictionary[DataContext].ToString());
            }
        }
        

        public object DataContext
        {
            get { return AssociatedObject.DataContext; }
        }
    }
}
